import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../../services/http.service';
@Component({
  selector: 'app-newslist',
  templateUrl: './newslist.component.html',
  styleUrls: ['./newslist.component.css']
})
export class NewslistComponent implements OnInit {
  public total: number = 100;
  public pageSize: number = 10;
  public pageIndex: number = 1;
  public loading:boolean = true;
  constructor(public http: HttpService) { }

  listOfSelection = [
    {
      text: 'Select All Row',
      onSelect: () => {
        this.checkAll(true);
      }
    },
    {
      text: 'Select Odd Row',
      onSelect: () => {
        this.listOfDisplayData.forEach((data, index) => (this.mapOfCheckedId[data.id] = index % 2 !== 0));
        this.refreshStatus();
      }
    },
    {
      text: 'Select Even Row',
      onSelect: () => {
        this.listOfDisplayData.forEach((data, index) => (this.mapOfCheckedId[data.id] = index % 2 === 0));
        this.refreshStatus();
      }
    }
  ];
  isAllDisplayDataChecked = false;
  isIndeterminate = false;
  listOfDisplayData: any[] = [];
  listOfAllData: any[] = [];
  mapOfCheckedId: { [key: string]: boolean } = {};

  currentPageDataChange($event: Array<{ id: number; name: string; age: number; address: string }>): void {
    this.listOfDisplayData = $event;
    this.refreshStatus();
  }

  refreshStatus(): void {
    this.isAllDisplayDataChecked = this.listOfDisplayData.every(item => this.mapOfCheckedId[item.id]);
    this.isIndeterminate =
      this.listOfDisplayData.some(item => this.mapOfCheckedId[item.id]) && !this.isAllDisplayDataChecked;
  }

  checkAll(value: boolean): void {
    this.listOfDisplayData.forEach(item => (this.mapOfCheckedId[item.id] = value));
    this.refreshStatus();
  }
  pageChange() {
    this.loading = true ;
    this.http.get('https://www.mxnzp.com/api/news/list?typeId=509&page='+ this.pageIndex).then((res:any) => {
      //  console.log(this.pageIndex);
      //console.log(res)
      this.listOfDisplayData = res.data;
      this.loading = false;
      //console.log(this.listOfDisplayData);
      console.log(this.pageIndex)
    });
    // console.log(e);
    // setInterval(()=>{

    //   console.log('run')
    // },200);
    //console.log(1);
  }
  ngOnInit(): void {
    this.pageChange();
    // for (let i = 0; i < 100; i++) {
    //   this.listOfAllData.push({
    //     id: i,
    //     name: `Edward King ${i}`,
    //     age: 32,
    //     address: `London, Park Lane no. ${i}`
    //   });
    // }
  }

}
